﻿
using UnityEngine;

public class StateHit : IState{
    public void Enter(EntityBase entity, params object[] args){

    }

    public void Process(EntityBase entity, params object[] args){
        entity.SetDir(Vector2.zero);
        entity.SetAction(Constants.ActionHit);
        
        TimerSvc.Instance.AddTimerTask((int tid) => {
           entity.SetAction(Constants.ActionDefault); 
           entity.Idle();
        }, (int)(GetHitAniLength(entity) * 1000));
    }

    public void Exit(EntityBase entity, params object[] args){
        
    }

    private float GetHitAniLength(EntityBase entity){
        AnimationClip[] clips = entity.GetAniClips();
        for (int i = 0; i < clips.Length; i++){
            string clipName = clips[i].name;
            if (
                clipName.Contains("hit") ||
                clipName.Contains("Hit") || 
                clipName.Contains("HIT")
            ){
                return clips[i].length;
            }
        }
        //保护值
        return 1;
    }
}
